﻿using AutoMapper;
using B.S.Backtrack.Domain.Entities;
using B.S.Backtrack.ErrorCode;
using B.S.Backtrack.Infrastructure.Interface;
using B.S.Backtrack.Read.API.Application.Command.Product;
using B.S.Backtrack.Read.API.Dtos;
using MediatR;

namespace B.S.Backtrack.Read.API.Application.CommandHandler.Product
{
    public class QueryInformationHandler : IRequestHandler<QueryInformationCommand, ApiResult<List<ProductsDTO>>>
    {
        private readonly IBaseRepository<Products> proRep;
        private readonly IBaseRepository<Goods> gRep;
        private readonly IMapper mapper;

        public QueryInformationHandler(IBaseRepository<Products> proRep,IBaseRepository<Goods> gRep,IMapper mapper)
        {
            this.proRep = proRep;
            this.gRep = gRep;
            this.mapper = mapper;
        }
        public Task<ApiResult<List<ProductsDTO>>> Handle(QueryInformationCommand request, CancellationToken cancellationToken)
        {
           ApiResult<List<ProductsDTO>> result = new ApiResult<List<ProductsDTO>>();
            var list = from a in proRep.GetAll()
                       join b in gRep.GetAll() on a.GoodId equals b.GoodId
                       where a.ProductId==request.ProductId
                       select new ProductsDTO
                       {
                           ProductId = a.ProductId,
                           GoodId = a.GoodId,
                           GoodName = b.GoodName,
                           IsDel = a.IsDel,
                           PictureId = a.PictureId,
                           ProductionBatch = a.ProductionBatch,
                           ProductionNum = a.ProductionNum,
                           ProductionEnterprise = a.ProductionEnterprise,
                           ShelfLife = a.ShelfLife,
                           GoverImage = a.GoverImage,

                       };
            result.Code = ApiEnums.查询成功;
            result.Msg=ApiEnums.查询成功.ToString();
            result.Data = list.ToList();
            return Task.FromResult(result);
        }
    }
}
